import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:jicaimao_flutter/utils/api_address.dart';
import 'package:jicaimao_flutter/utils/http_utils.dart';
import 'package:jicaimao_flutter/entity/mybox.dart';
import 'package:jicaimao_flutter/utils/sp_utils.dart';
import 'package:jicaimao_flutter/utils/sp_key.dart';
import 'package:jicaimao_flutter/utils/toast.dart';
import 'package:jicaimao_flutter/views/mine/account.dart';
import 'package:jicaimao_flutter/views/mine/check_in.dart';
import 'package:jicaimao_flutter/views/mine/check_out.dart';
class MyTreasureBox extends StatefulWidget {
  @override
  _MyTreasureBoxState createState() => new _MyTreasureBoxState();
}

class _MyTreasureBoxState extends State<MyTreasureBox> {

var totolAcccout;
var userAccout;
var benAccout;
var walletAdress;
  @override
  void initState() {
    super.initState();
    _loadData();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  new Scaffold(
        bottomSheet: _bottomSheet(context),
        body: new Container(
          child: Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Image.asset('images/bg_img.png'),
                  Positioned(
                    left: 1.0,
                    top: 1.0,
                    right: 1.0,
                    child: AppBar(
                        centerTitle: true,
                        backgroundColor: Colors.transparent,
                        elevation: 100.0,
                        iconTheme: IconThemeData(
                          color: Colors.white,
                        ),
                        actions: <Widget>[
                          IconButton(
                              icon: Image.asset('images/img_order.png'),
                              onPressed: (){
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(builder: (context) => Account()),
                                );
                              })
                        ],
                        title: new Text(
                          '我的宝箱',
                          style: new TextStyle(color: Colors.white),
                        )
                    ),
                  ),
                  Positioned(
                     left: 10.0,
                      right: 10.0,
                      top: 95.0,
                      child: Image.asset('images/finaancial_head.png',width: 114.0,height: 114.0,)
                  ),
                  Positioned(
                      left: 10.0,
                      right: 10.0,
                      top: 125.0,
                      child: Column(
                        children: <Widget>[
                          Text("总资产",style: TextStyle(
                            color: Colors.white,
                            fontSize: 18.0
                          ),),
                          Text(null != totolAcccout ?totolAcccout.toStringAsFixed(4):"0.0000",style: TextStyle(
                              color: Colors.white,
                              fontSize: 20.0
                          ),)
                        ],
                      ))
                  
                ],
              ),
              Container(

                color: Colors.white,
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.all(15.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: new Column(

                        children: <Widget>[
                          Text('可用资产（BSC）',style: TextStyle(
                              color: Colors.black,
                              fontSize: 16.0
                          ),),
                          Text('本金',style: TextStyle(
                              color: Color(0xFFF38E3C),
                              fontSize: 15.0
                          ),)
                        ],
                        crossAxisAlignment: CrossAxisAlignment.start,
                      ),
                      flex: 1,),
                    Text(null != userAccout ?userAccout.toStringAsFixed(4):"0.0000",style: TextStyle(
                      color: Color(0xFFF38E3C),
                      fontSize: 15.0
                    ),)
                  ],
                ),
              ),
              Container(

                color: Colors.white,
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.all(15.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: new Column(

                        children: <Widget>[
                          Text('理财资产（BSC）',style: TextStyle(
                              color: Colors.black,
                              fontSize: 16.0
                          ),),
                          Text('本金',style: TextStyle(
                              color: Color(0xFFF38E3C),
                              fontSize: 15.0
                          ),)
                        ],
                        crossAxisAlignment: CrossAxisAlignment.start,
                      ),
                      flex: 1,),
                    Text(null != benAccout ?benAccout.toStringAsFixed(4):"0.0000",style: TextStyle(
                        color: Color(0xFFF38E3C),
                        fontSize: 15.0
                    ),)
                  ],
                ),
              ),
            ],
          ),
        ),
      );

  }

  void _loadData() async {
     String url = NetAddress.myBoxList;
     String userId = SpUtils.getString(SpKey.KEY_USER_ID);
     print("key    =====${userId}");
     print("key=${userId}");
     var data = {'userId': userId};
     var respone = await HttpUtil().post(url,data: data);
     mybox _mybox = mybox.fromJson(json.decode(respone));


     setState(() {

       if(_mybox.code == "200"){
         totolAcccout = _mybox.result.sum;
         userAccout = _mybox.result.available;
         benAccout = _mybox.result.financial;
         print(_mybox.result.available);
         SpUtils.putString(SpKey.KEY_BOX_ADRESS, _mybox.result.walletAddress);
         SpUtils.putString(SpKey.KEY_BOX_BSCADRESS, _mybox.result.purseAddress);
       }else{
         Toast.toast(context, _mybox.msg);

       }

  
     });

  }


  Widget _bottomSheet(BuildContext context){
    return Container(

      child: Row(
        children: <Widget>[
          Expanded(child: GestureDetector(
            child: Container(
              height: 50.0,
              child: Center(
                child: Text('转入',style: TextStyle(fontSize: 18.0),),
              ),
              color: Colors.white,
            ),
            onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) => CheckIn()));
            },
          ),flex: 1,),
          Expanded(child: GestureDetector(
            child: Container(
              height: 50.0,
              color: Color(0xFFad5afe),
              child: Center(
                child: Text('转出',style: TextStyle(fontSize: 18.0,color: Colors.white),),
              ),
            ),
            onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) => CheckOut()));
            },
          ),flex: 1,),
        ],
      ),
    );
  }

}
